package com.leoman.plz_p.views.mine;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.leoman.plz_p.MainActivity;
import com.leoman.plz_p.R;
import com.leoman.plz_p.interfaces.MineView;
import com.leoman.plz_p.module.bean.User;
import com.leoman.plz_p.presenter.MinePresenter;
import com.leoman.plz_p.utils.UserHelper;
import com.leoman.plz_p.utils.authority.AuthorityContext;
import com.leoman.plz_p.views.WebActivity;
import com.leoman.plz_p.weidget.NavBar;
import com.leoman.plz_p.weidget.RoundedTransformationBuilder;

import butterknife.Bind;
import butterknife.OnClick;
import cn.com.corelib.base.BaseFragment;

/**
 * Created by guoh on 2018/8/26.
 */
public class MineFragment extends BaseFragment<MineView, MinePresenter> implements MineView {

    @Bind(R.id.nav)
    NavBar nav;
    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.tv_name)
    TextView tvName;

    @Bind(R.id.ll_about_us)
    LinearLayout llAboutUs;
    @Bind(R.id.ll_help)
    LinearLayout llHelp;
    @Bind(R.id.ll_log_out)
    LinearLayout llLogOut;
    @Bind(R.id.tv_change_password)
    TextView tvChangePassword;

    @Override
    protected int getLayoutId() {
        return R.layout.fr_mine;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        nav.setTitle("个人中心");
        nav.hideBack();
        tvChangePassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    @Override
    protected MinePresenter createPresenter() {
        return new MinePresenter();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (AuthorityContext.getContext().isLoggedIn()) {
            User user = UserHelper.getSavedUser();
            if (!TextUtils.isEmpty(user.getHeadimg())) {
                Glide.with(getViewContext()).load(user.getHeadimg()).centerCrop().transform(new CenterCrop(getViewContext()), new RoundedTransformationBuilder().oval(true).build(getViewContext())).into(ivHead);
            }
            tvName.setText(user.getNickname());
        }
    }

    @OnClick({R.id.iv_head,R.id.tv_change_password, R.id.tv_name, R.id.ll_account, R.id.ll_about_us, R.id.ll_help, R.id.ll_log_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
            case R.id.tv_name:
                startActivity(PersonalInfoActivity.getLaunchIntent(getViewContext()));
                break;
            case R.id.tv_change_password:
                startActivity(ChangePasswordActivity.getLaunchIntent(getViewContext()));
                break;
            case R.id.ll_account:
                startActivity(PersonalInfoActivity.getLaunchIntent(getViewContext()));
                break;
            case R.id.ll_about_us:
                getPresenter().getAboutUs();
                break;
            case R.id.ll_help:
                getPresenter().getHelp();
                break;
            case R.id.ll_log_out:
                new MaterialDialog.Builder(getViewContext())
                        .title("提示")
                        .content("确定要退出登录吗?")
                        .positiveText("退出")
                        .negativeText("再想想")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                                UserHelper.clear();
                                AuthorityContext.getContext().loggedOut();
                                ((MainActivity)getActivity()).setCurrentPage(0);
                            }
                        })
                        .show();
                break;
        }
    }

    @Override
    public void obtainAboutUs(String content) {
        startActivity(WebActivity.getLaunchIntent(getViewContext(), "关于我们", null, content, false,false));
    }

    @Override
    public void obtainHelp(String content) {
        startActivity(WebActivity.getLaunchIntent(getViewContext(), "帮助中心", null, content, false,false));
    }


}
